
####
# Replication code for "The Climate Advocacy Gap"
# Jonas Meckling; Samuel Trachtman
# Climatic Change
####

rm(list=ls())
setwd("C:/Users/samtr/Dropbox/IGs in state climate politics/mapping pro-climate IGs/replication")

#libraries
library(rio)
library(tidyverse)
library(ggrepel)
library(ggeasy)
library(ggpubr)



#import data sets
groups_data <- import("groups_data.xlsx")
st.df <- import("states_data.xlsx")


#total count (note: this excludes conservation)
st.df$count <- st.df$count_energy + st.df$count_env
st.df$records <- st.df$records_energy + st.df$records_env
st.df$amount <- st.df$amount_energy + st.df$amount_env


#proportion of total
st.df$prop_clean_energy <- st.df$count_energy/st.df$num_energy_groups
st.df$prop_climate_total <-  st.df$count/st.df$num_groups
st.df$prop_env_energy <- st.df$count_env/st.df$num_energy_groups
st.df$prop_env_all <- st.df$count_env/st.df$num_groups
st.df$prop_climate_energy <- (st.df$count_energy + st.df$count_env)/st.df$num_energy_groups

#region-level stats
region.df <- st.df %>%
  group_by(region) %>%
  dplyr::summarize(propall = sum(count)/sum(num_groups),
                   propenergy = sum(count_energy)/sum(num_energy_groups))


#fig1
st.df$temp_energy <- st.df$num_energy_groups - st.df$count_energy - st.df$count_env #this is for making plot with all 3, since need total less environmental and clean energy
st.df$perc_energy <- st.df$count_energy/st.df$num_energy_groups
st.df$perc_env <- st.df$count_env/st.df$num_energy_groups

gathered <- st.df %>% 
  gather(count_env, count_energy, num_energy_groups, temp_energy, perc_energy, perc_env, key=type, value=total) %>%
  dplyr::select(abb, type, total) 
gathered$group <- ifelse(gathered$type %in% c("count_env", "perc_env"), "environmental",
                         ifelse(gathered$type %in% c("count_energy", "perc_energy"), "clean energy", "total energy and natural resources"))
gathered2 <- subset(gathered, type %in% c("perc_env", "perc_energy"))
gathered2$total <- gathered2$total*100

#order states
temp <- gathered2 %>%
  group_by(abb) %>%
  summarize(total2 = sum(total))

#pro climate groups percentage by state
groups_percentage <- ggplot(data = gathered2,
                        mapping = aes(x = total, y = reorder(abb, total), fill = group))+
  geom_vline(xintercept = 0, color = "gray30") +
  geom_col()+
  scale_fill_manual(values = c("sky blue","#669933", "grey")) +
  labs(x="Percentage of total energy interests", y="")+
  ggtitle("(a)")+
  ggeasy::easy_center_title()+
  theme_minimal()+
  theme(legend.position = "none")+
  theme(legend.title=element_blank())
groups_percentage

#total groups lobbying
pdat <- subset(gathered, type %in% c("count_energy", "count_env"))
pdat <- left_join(pdat, temp, by="abb")
groups_total <- ggplot(data = pdat,
                       mapping = aes(x = total, y = reorder(abb, total2), fill = forcats::fct_rev(group)))+
  geom_vline(xintercept = 0, color = "gray30") +
  geom_col()+
  scale_fill_manual(values = c("sky blue","#669933", "grey")) +
  labs(x="Number of groups lobbying", y="")+
  ggtitle("(b)")+
  ggeasy::easy_center_title()+
  theme_minimal()+
  theme(legend.position = "right")+
  theme(legend.title=element_blank())
groups_total

tot_wdenom <- ggarrange(groups_percentage, groups_total, common.legend = TRUE, legend="right")
tot_wdenom
ggsave("tot_wdenom.png", tot_wdenom, height=8, width=9)



#fig2
advocacy_demvote <- ggplot(data = st.df, aes(x=100*percent_dem, y= 100*prop_env_energy)) + 
  geom_point(color="blue")+
  geom_text_repel(aes(label=abb),size=3)+
  geom_smooth(method="lm", se=F, color="red")+
  xlim(20,65)+
  ylim(0,22)+ #exclude outlier MA 
  xlab("Democratic presidential vote share (2016)")+
  ylab("Environmental groups as percentage of energy interests lobbying")+
  theme_minimal()+
  ggtitle("(a)")+
  ggeasy::easy_center_title()
advocacy_demvote

green_energy_demvote <- ggplot(data = st.df, aes(x=100*percent_dem, y= 100*prop_clean_energy)) + 
  geom_point(color="blue")+
  geom_text_repel(aes(label=abb),size=3)+
  geom_smooth(method="lm", se=F, color="red")+
  xlim(20,65)+
  ylim(0,22)+ #exclude outlier MA
  xlab("Democratic presidential vote share (2016)")+
  ylab("Clean energy interests as percentage of energy interests lobbying")+
  theme_minimal()+
  ggtitle("(b)")+
  ggeasy::easy_center_title()
green_energy_demvote

vote_scatters <- grid.arrange(advocacy_demvote, green_energy_demvote, ncol = 2)
ggsave("vote_scatters.png", vote_scatters, width=9, height=6)

#fig3
advocacy_emissions <- ggplot(data = st.df, aes(x=emissions, y= 100*prop_env_energy)) + 
  geom_point(color="blue")+
  geom_text_repel(aes(label=abb),size=3)+
  geom_smooth(method="lm", se=F, color="red")+
  xlim(0,250)+
  ylim(0,22)+ #cutting out CA, TX outliers. 
  xlab("Million metric tons of energy-related CO2")+
  ylab("Environmental groups as percentage of groups lobbying")+
  theme_minimal()+
  ggtitle("(a)")+
  ggeasy::easy_center_title()
advocacy_emissions

clean_energy_emissions <- ggplot(data = st.df, aes(x=emissions, y= 100*prop_clean_energy)) + 
  geom_point(color="blue")+
  geom_text_repel(aes(label=abb),size=3)+
  geom_smooth(method="lm", se=F, color="red")+
  xlim(0,250)+
  ylim(0,22) + #ylim(0,3)+ #cutting out CA, TX outliers. 
  xlab("Million metric tons of energy-related CO2")+
  ylab("Clean energy groups as percentage of energy groups lobbying")+
  theme_minimal()+
  ggtitle("(b)")+
  ggeasy::easy_center_title()
clean_energy_emissions

emissions_scatters <- grid.arrange(advocacy_emissions, clean_energy_emissions, ncol = 2)
ggsave("emissions_scatters.png", emissions_scatters, width=9, height=6)



#fig SI1
st.df$clean_energy_ratio <- st.df$count_energy/(st.df$count_energy + st.df$count_env)
clean_energy_ratio <- ggplot(data = st.df, aes(x=100*percent_dem, y= 100*clean_energy_ratio)) + 
  geom_point(color="blue")+
  geom_text_repel(aes(label=abb),size=3)+
  geom_smooth(method="lm", se=F, color="red")+
  xlim(20,65)+
  #ylim(10,90)+ #exclude outlier MA
  xlab("Democratic presidential vote share (2016)")+
  ylab("Clean energy interests as percentage of pro-climate interests lobbying")+
  theme_minimal()+
  #ggtitle("(b)")+
  ggeasy::easy_center_title()
ggsave("clean_energy_ratio.png", clean_energy_ratio, width=6, height=6)


#fig SI2
st.df$dem_seats <- as.numeric(st.df$dem_seats)
advocacy_demleg <- ggplot(data = st.df, aes(x=100*dem_seats, y= 100*prop_env_energy)) + 
  geom_point(color="blue")+
  geom_text_repel(aes(label=abb),size=3)+
  geom_smooth(method="lm", se=F, color="red")+
  ylim(0,22)+ #exclude outlier MA 
  xlab("Proportion of state legislative seats held by Democrats")+
  ylab("Environmental groups as percentage of energy interests lobbying")+
  theme_minimal()+
  ggtitle("(a)")+
  ggeasy::easy_center_title()

green_energy_demleg <- ggplot(data = st.df, aes(x=100*dem_seats, y= 100*prop_clean_energy)) + 
  geom_point(color="blue")+
  geom_text_repel(aes(label=abb),size=3)+
  geom_smooth(method="lm", se=F, color="red")+
  ylim(0,22)+ #exclude outlier MA
  xlab("Proportion of state legislative seats held by Democrats")+
  ylab("Clean energy interests as percentage of energy interests lobbying")+
  theme_minimal()+
  ggtitle("(b)")+
  ggeasy::easy_center_title()

leg_scatters <- grid.arrange(advocacy_demleg, green_energy_demleg, ncol = 2)
ggsave("leg_scatters.png", leg_scatters, width=9, height=6)





